package net.ufrog.common.jetbrick;

import jetbrick.template.loader.ClasspathResourceLoader;
import jetbrick.template.web.springmvc.JetTemplateViewResolver;
import net.ufrog.common.app.App;
import net.ufrog.common.app.PropertiesValue;
import net.ufrog.common.exception.ServiceException;

import java.util.Properties;

/**
 * 配置简化工具
 *
 * @author ultrafrog, ufrog.net@gmail.com
 * @version 0.1, 2017-07-04
 * @since 0.1
 */
public class SpringJetxConfigurations {

    private static final String KEY_JETX_LOADER         = "jetx.template.loaders";
    private static final String KEY_JETX_LOADER_ROOT    = "jetx.template.loaders.root";

    /**  */
    private SpringJetxConfigurations() {}

    /**
     * @return jet template view resolver
     */
    public static JetTemplateViewResolver jetTemplateViewResolver() {
        JetxProperties jp = App.bean("jetx", JetxProperties.class);
        JetTemplateViewResolver jetTemplateViewResolver = new JetTemplateViewResolver();

        if (jp != null) {
            jetTemplateViewResolver.setContentType(jp.getContentType());
            jetTemplateViewResolver.setPrefix(jp.getPrefix());
            jetTemplateViewResolver.setSuffix(jp.getSuffix());
            jetTemplateViewResolver.setOrder(jp.getOrder());
            if (!jp.getConfig().containsKey(KEY_JETX_LOADER)) {
                jp.getConfig().put(KEY_JETX_LOADER, ClasspathResourceLoader.class.getName());
            }
            jetTemplateViewResolver.setConfigProperties(jp.getConfig());
            return jetTemplateViewResolver;
        } else {
            throw new ServiceException("cannot config jet template.");
        }
    }

    /**
     * 模版属性
     *
     * @author ultrafrog, ufrog.net@gmail.com
     * @version 0.1, 2017-07-04
     * @since 0.1
     */
    public static class JetxProperties {

        @PropertiesValue(value = "content-type", defaultValue = "text/html; charset=utf-8")
        private String contentType;

        @PropertiesValue(value = "order", defaultValue = "9")
        private Integer order;

        @PropertiesValue(value = "prefix", defaultValue = "templates/default/")
        private String prefix;

        @PropertiesValue(value = "suffix", defaultValue = ".html")
        private String suffix;

        @PropertiesValue(value = "config")
        private Properties config;

        public String getContentType() {
            return contentType;
        }

        public void setContentType(String contentType) {
            this.contentType = contentType;
        }

        public Integer getOrder() {
            return order;
        }

        public void setOrder(Integer order) {
            this.order = order;
        }

        public String getPrefix() {
            return prefix;
        }

        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }

        public String getSuffix() {
            return suffix;
        }

        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }

        public Properties getConfig() {
            return config;
        }

        public void setConfig(Properties config) {
            this.config = config;
        }
    }
}
